package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.ColdDirectReq;
import com.ruibang.glass.quality.domain.resp.PointDefect;
import com.ruibang.glass.quality.domain.resp.ProportionInfo;
import com.ruibang.glass.quality.entity.EveryHourColdDirectLoss;
import com.ruibang.glass.quality.mapper.EveryHourColdDirectLossMapper;
import com.ruibang.glass.quality.service.EveryHourColdDirectService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EveryHourColdDirectServiceImpl extends ServiceImpl<EveryHourColdDirectLossMapper, EveryHourColdDirectLoss> implements EveryHourColdDirectService {

    @Override
    public ResultBody queryEveryHourColdDirect(ColdDirectReq coldDirectReq) {
        final IPage<EveryHourColdDirectLoss> page = new Page<>(coldDirectReq.getPageNo(), coldDirectReq.getPageSize());
        return ResultBody.success(baseMapper.queryEveryHourColdDirect(page, coldDirectReq));
    }

    @Override
    public List<EveryHourColdDirectLoss> queryTodayData(String today) {
        return baseMapper.queryTodayData(today);
    }

    @Override
    public ResultBody queryCheckAverage(ColdDirectReq coldDirectReq) {
        return ResultBody.success(baseMapper.queryCheckAverage(coldDirectReq));
    }

    @Override
    public ResultBody queryDefect(ColdDirectReq coldDirectReq) {
        List<EveryHourColdDirectLoss> directLosses = baseMapper.queryDefect(coldDirectReq);
        if (CollectionUtils.isNotEmpty(directLosses)) {
            long platinum = 0;
            long stone = 0;
            long bubble = 0;
            for (EveryHourColdDirectLoss directLoss : directLosses) {
                platinum = platinum + directLoss.getPlatinumTotalNum();
                stone = stone + directLoss.getStoneTotalNum();
                bubble = bubble + directLoss.getBubbleTotalNum();
            }

            ProportionInfo info = new ProportionInfo();
            info.setPlatinum(platinum);
            info.setStone(stone);
            info.setBubble(bubble);
            return ResultBody.success(info);
        }
        return ResultBody.success();
    }

    /**
     * @Description: 点状图
     * @version v1.0
     * @author songJinKang
     * @date 2023-10-18 8:54
     */
    @Override
    public ResultBody getPointDefectData(ColdDirectReq coldDirectReq) {
        List<PointDefect> pointDefects = baseMapper.getPointDefectData(coldDirectReq);
        if (CollectionUtils.isNotEmpty(pointDefects)) {
            Map<String, List<PointDefect>> pointMap = pointDefects.stream().collect(Collectors.groupingBy(p -> p.getDefectName()));
            return ResultBody.success(pointMap);
        }
        return ResultBody.success();
    }

    /**
     * @Description: 根据时间获取数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-15 11:22
     */
    @Override
    public List<EveryHourColdDirectLoss> getByTime(ArrayList<String> time) {
        QueryWrapper<EveryHourColdDirectLoss> hlQw = new QueryWrapper<>();
        hlQw.lambda().in(EveryHourColdDirectLoss::getDirectTime, time);
        return this.list(hlQw);
    }
}
